package com.ruoyi.framework.web.service;

import cn.dev33.satoken.stp.StpInterface;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SaTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@Component
public class SaInterface implements StpInterface {

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private RedisCache redisCache;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        ArrayList<Object> menuPermission = new ArrayList<>();
        //缓存版本 问题：管理员更新时，不会同步
//        Object o = StpUtil.getSession().get("user-menu");
//        if (o == null) {
//            List<String> menuPermission = new ArrayList<>(permissionService.getMenuPermission(SaTokenUtils.getUser().getUser()));
//            StpUtil.getSession().set("user-menu", menuPermission);
//            return menuPermission;
//        }
//        return (List<String>) o;
        //缓存版本2 采用redis将所有的role与menu都缓存起来，管理员修改时就更新对应的role缓存

        LoginUser user = SaTokenUtils.getUser();
        // 微信用户 不需要权限
        if (user == null) {
            return new ArrayList<>();
        }
        return new ArrayList<>(permissionService.getMenuPermission(SaTokenUtils.getUser().getUser()));
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {

        if(SaTokenUtils.getUser() == null){
            return new ArrayList<>();
        }

        Set<String> rolePermission = permissionService.getRolePermission(SaTokenUtils.getUser().getUser());
        return new ArrayList<>(rolePermission);
    }
}
